<?php
/*
    OpenLibrary
    Copyright (C) 2011  Michael Gukov, Pham Thi Thuy Duong

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


namespace App\Views\Lib;

use Sun\Sun;
use Sun\Html;
use Sun\Html\Form;
use App\Models;
use App\Models\DB;

class EditType extends Main {
    public function __construct() {
        parent::__construct();
        $this->data_scene = 'EditTypeList';
    }

    public function AltScene() {
        if (count($this->cond) == 0) {
            $libdb = new Models\DB();
            $query = "SELECT name FROM edit_type WHERE type_id = '{$this->data['id']}'";
            $name = $libdb->SafeQuery($query)->fetch(DB::FETCH_NUM);
            $query = "SELECT field_id FROM type_field_rel WHERE type_id = '{$this->data['id']}'";

            $fields = $libdb->query($query)->fetchAll(DB::FETCH_NUM);
            $str = $fields[0][0];
            for ($i = 0; $i < count($fields); ++$i) {
                $str .= ",".$fields[$i][0];
            }
            $this->data['fields'] = $str;
            $this->data['name'] = $name[0];
        }
        $this->IncludeScene("AltBody");
    }


    public function EditTypes() {
        $edit = new Models\Edition();
        $options = "";

        $types = $edit->GetEditionTypes();
        $sel = new Form\Select(array(
            'id' => "EditTypeList",
            'class' => "WideListBox",
            'size' => "20"));
        foreach ($types as $type) {
            $sel->AddOption($type['type_id'], $type['name']);
        }
        $sel->First()->selected = "selected";
        return $sel;
    }

    public function FieldList($id, $sel = false) {

        $sel_fields = array();
        if (isset($this->data['fields']))
            $sel_fields = explode(',', $this->data['fields']);

        $db = new Models\DB();
        $fields = $db->Table('field')->fetchAll(DB::FETCH_ASSOC);

        $select = new Html\Form\Select(array(
            'class' => 'MiddleWide',
            'id' => $id,
            'multiple' => 'multiple',
            'size' => '15'
        ));

        foreach ($fields as $field) {
            if ($sel && array_search($field['field_id'], $sel_fields) !== false) {
                $select->AddOption($field['field_id'], $field['name']); }
            else if (!$sel && array_search($field['field_id'], $sel_fields) === false) {
                $select->AddOption($field['field_id'], $field['name']); }
        }
        return $select;
    }

}
